

namespace apeng {
	export class JS {
		/**
		 * 获取枚举 value为number的值
		 * @param enums 
		 * @param cb 
		 * @returns 
		 */
		public static getEnumValue(enums: any, cb?: (value: number) => void): number[] {
			let values: number[] = []
			for (let key in enums) {
				let value = enums[key]
				if (typeof value == "number") {
					values.push(value)
					if (cb)
						cb(value)
				}
			}
			return values
		}

		public static getEnumByNums(enums: any): number[] {
			if (!enums["__num__"]) {
				enums["__num__"] = []
				for (let key in enums) {
					let value = enums[key]
					if (typeof value == "number")
						enums["__num__"].push(value)
				}
				// 越小的越在前
				enums["__num__"].sort((a: any, b: any) => a - b)
			}
			return enums["__num__"]
		}

		public static async promiseDelay(delay: number) {
			return new Promise(res => setTimeout(res, delay * 1000))
		}

		/**深拷贝类 只拷贝一层 */
		public static minxi<T>(value: T): T {
			if (value === null || value === undefined || value === (0 as any) || value === ("" as any))
				return value
			let target = null

			if (typeof value === "object")
				target = this._mixin(Array.isArray(value) ? [] : {}, value)
			else
				target = value

			return target as T
		}

		public static deep<T>(value: T): T {
			if (value == null)
				return value
			if (typeof value == "object")
				return JSON.parse(JSON.stringify(value))
			return value
		}

		public static parseArray<T>(str: string): T[] {
			if (!str)
				return []
			return JSON.parse(str)
		}


		public static _copyprop(name: string, source: any, target: any) {
			let pd = this._getPropertyDescriptor(source, name) as any
			Object.defineProperty(target, name, pd)
		}

		public static _getPropertyDescriptor(obj: any, name: any) {
			while (obj) {
				let pd = Object.getOwnPropertyDescriptor(obj, name)
				if (pd)
					return pd
				obj = Object.getPrototypeOf(obj)
			}
			return null
		}

		/**
	   * 判断是否为空对象
	   * @param {Object} obj
	   * @returns {boolean}
	   * @memberof Maps
	   */
		public static isNullObj(obj: Object): boolean {
			if (!obj)
				return false
			// if (Object.keys(obj).length != 0)
			// 	return false;
			for (let key in obj)
				return false

			return true
		}

		public static isValid(value: any): boolean {
			if (value == null || value == undefined || value === false)
				return false
			return true
		}

		private static _mixin(obj: any, ...sourceObj: any[]) {
			obj = obj || {}
			for (let i = 1, length = arguments.length; i < length; i++) {
				let source = arguments[i]
				if (source) {
					if (typeof source !== "object")
						continue

					for (let name in source)
						this._copyprop(name, source, obj)

				}
			}
			return obj
		}

		/**
	 * @param filename  文件名   a.txt
	 * @param content 内容
	 * @param contentType  头部
	 */
		public static download(filename: string, content: string, contentType?: string) {
			if (!contentType) contentType = 'application/octet-stream';
			var a = document.createElement('a');
			var blob = new Blob([content], { 'type': contentType });
			a.href = window.URL.createObjectURL(blob);
			a.download = filename;
			a.click();
		}


		/**浮点数限定位数 */
		public static numCount(num: number, count: number) {
			let mul = Math.pow(10, count)
			return Math.floor(num * mul) / mul
		}

		public static isPromise(obj: any): boolean {
			return !!obj  //有实际含义的变量才执行方法，变量null，undefined和''空串都为false
				&& (typeof obj === 'object' || typeof obj === 'function') // 初始promise 或 promise.then返回的
				&& typeof obj.then === 'function'
		}


	}
}